package cn.xdf.controller;

import cn.xdf.entity.City;
import cn.xdf.entity.Province;
import cn.xdf.entity.Student;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

//@Controller+@ResponseBody//将结果集以json形式响应
@RequestMapping("/demo01")
@RestController
public class Demo01Controller {

//    @GetMapping("/demo01")
    @PostMapping("/demo01")
    public void demo01(
            //当表单项目名称和形参不一样可以使用@Requetpart注解来指定名称
            @RequestPart("pic")MultipartFile pic,
    HttpServletResponse response)throws Exception{
        //表单项的名称
        String name=pic.getName();

        //文件的名称
        String fileName = pic.getOriginalFilename();

        //文件的大小
        long size = pic.getSize();
        //文件MIME类型
        String contentType = pic.getContentType();

        //将前端传递的文件写入到磁盘
        pic.transferTo(new File("D:/aaa/"+ UUID.randomUUID()+".png"));

        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("<hr/> name:"+name);
        response.getWriter().write("<hr/>fileName:"+fileName);
        response.getWriter().write("<hr/>size:"+size);
        response.getWriter().write("<hr/> contentType:" +contentType);

    }

    //多文件上传  list<MultipartFile>
    @PostMapping("/demo02")
    public String demo02(@RequestPart("pics")List<MultipartFile> pic)throws Exception{

        for(MultipartFile multipartFile:pic){
            multipartFile.transferTo(new File("D:/aaa/"+UUID.randomUUID()+".png"));
        }
        return "ok";
    }


}
